package ace.cmp.i18n.core.impl;

import ace.cmp.i18n.api.AceLocaleResolver;
import ace.cmp.i18n.api.AceMessageSource;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;

/**
 * @author caspar
 * @date 2023/8/10 14:19 扩展 {@link MessageSource}
 */
public class DefaultMessageSource implements AceMessageSource {
  private final MessageSource delegating;
  private final AceLocaleResolver localeResolver;

  public DefaultMessageSource(MessageSource delegating, AceLocaleResolver localeResolver) {
    this.delegating = delegating;
    this.localeResolver = localeResolver;
  }

  @Override
  public String getMessage(String code, Object[] args, String defaultMessage) {
    return this.delegating.getMessage(
        code, args, defaultMessage, this.localeResolver.resolveLocale());
  }

  @Override
  public String getMessage(String code, Object[] args) throws NoSuchMessageException {
    return this.delegating.getMessage(code, args, this.localeResolver.resolveLocale());
  }
}
